﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NHibernate;
using ZLCMS.Business;
using ZLCMS.Data.Model;
using ZLCMS.Business.DwzFrame;
using DotNet.Common.Command;
using ZLCMS.Commands.Sys.Commands.Role;

namespace ZLCMS.Web.SystemManage.Basics.RightConfig.Role
{
    public partial class RoleDetail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //初始化页面
            if (!IsPostBack)
            {
                int uid = 0;
                if (Int32.TryParse(Request["uid"], out uid))
                {
                    using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
                    {
                        UrRole role = session.Get<UrRole>(uid);
                        if (role != null)
                        {
                            name.Text = role.RoleName;
                            code.Text = role.RoleCode;
                            ViewState["uid"] = uid;
                        }
                    }
                }
            }
            else
            {
                SaveRoleParameter param = new SaveRoleParameter();
                param.Name = name.Text;
                param.RoleId = ViewState["uid"] == null ? 0 : (int)ViewState["uid"];

                ICommandResult<SaveRoleResult> result = new SaveRoleCommand().Execute(param);

                FrameHelper _framePage = new FrameHelper(this);
                ReturnCode rc = new ReturnCode()
                {
                    statusCode = result.Error ? 300 : 200,
                    message = result.ErrorMsg
                };

                rc.callbackType = "forward";
                rc.forwardUrl = "/SystemManage/Basics/RightConfig/Role/RoleDetail.aspx?tabid=RoleDetail";

                if (Request["submit_op"] != "1" && !result.Error)
                {
                    UrRole role = result.Result.Role;
                    rc.forwardUrl += "&uid=" + role.Id;
                }

                _framePage.Alert(rc);
            }

        }
    }
}
